// 平行树关系中，实现根据 当前 id，统计出所有子部门id

const departmentList = [
    {
        "id": 0, 
        "name": "根", 
        "parentId": 0, 
    },
    { 
        "id": 1, 
        "name": "研发部", 
        "parentId": 0, 
    },
    { 
        "id": 2, 
        "name": "法务部", 
        "parentId": 0, 
    },
    { 
        "id": 3, 
        "name": "测试组", 
        "parentId": 1, 
    }, 
    { 
        "id": 4, 
        "name": "前端组", 
        "parentId": 1, 
    }, 
    { 
        "id": 5, 
        "name": "后端组", 
        "parentId": 1, 
    },
    {
        "id": 6, 
        "name": "web组", 
        "parentId": 4, 
    },
    {
        "id": 7, 
        "name": "安卓组", 
        "parentId": 4, 
    },
    {
        "id": 8, 
        "name": "ios组", 
        "parentId": 4, 
    }
];



let id = 4; // 当前部门id
let subDepartmentids = []; //子部门id 

function dg(_subDepartmentids) {
    const ids = [];
    for (const id of _subDepartmentids) {
        for (const item of departmentList) {
            if (id === item.parentId) {
                ids.push(item.id);
            }
        }
    }
    subDepartmentids = [...subDepartmentids, ...ids];
    if (ids.length > 0) {
        dg(ids);
    }
    
}
dg([id]);

console.log(subDepartmentids);// 得出所有子部门id